﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public static class SoundManager
    {
        // Audio Data        
        static Dictionary<string, SoundEffectInstance> soundBank;
        static Dictionary<string, Song> musicBank;

        public static void Initialize(Game game)
        {
            soundBank = new Dictionary<string, SoundEffectInstance>();
            musicBank = new Dictionary<string, Song>();
        }

        public static void LoadContent(ContentManager content)
        {
            SoundEffect soundEffect;
            SoundEffectInstance soundEffectInstance;

            Song song;
        }

        public static void PlaySound(string soundName)
        {
            if (soundBank.ContainsKey(soundName))
            {
                soundBank[soundName].Play();
            }
        }
        public static void PlaySound(string soundName, bool isLooped)
        {
            if (soundBank.ContainsKey(soundName))
            {
                if (soundBank[soundName].IsLooped != isLooped)
                    soundBank[soundName].IsLooped = isLooped;
                soundBank[soundName].Play();
            }
        }
        public static void PlaySound(string soundName, bool isLooped, float volume)
        {
            if (soundBank.ContainsKey(soundName))
            {
                if (soundBank[soundName].IsLooped != isLooped)
                    soundBank[soundName].IsLooped = isLooped;
                soundBank[soundName].Volume = volume;
                soundBank[soundName].Play();
            }
        }
        public static void StopSound(string soundName)
        {
            if (soundBank.ContainsKey(soundName))
                soundBank[soundName].Stop();
        }
        public static void StopSound()
        {
            foreach (SoundEffectInstance sound in soundBank.Values)
            {
                if (sound.State != SoundState.Stopped)
                {
                    sound.Stop();
                }
            }
        }

        public static void PlayMusic(string musicSoundName)
        {
            if (!MediaPlayer.GameHasControl)
            {
                return;
            }

            if (musicBank.ContainsKey(musicSoundName))
            {
                if (MediaPlayer.State != MediaState.Stopped)
                {
                    MediaPlayer.Stop();
                }

                try
                {
                    MediaPlayer.Play(musicBank[musicSoundName]);
                    MediaPlayer.IsRepeating = true;
                }
                catch (UnauthorizedAccessException) { }
                catch (InvalidOperationException) { }
            }
        }
        public static void StopMusic()
        {
            if (!MediaPlayer.GameHasControl)
            {
                return;
            }

            if (MediaPlayer.State != MediaState.Stopped)
                MediaPlayer.Stop();
        }
    }
}
